﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using NHibernate.Criterion;
using Project.Common;
using Project.Model;
using System.Linq;

namespace Web
{
	/// <summary>
	/// 功能：分页列表页面
	///       (Plantable)
	/// 作者：luky
	/// 日期：2010-12-28
	/// </summary>
    public partial class Plantable_List : AdminPage
    {
        //创建查询器
        IList<ICriterion> queryList = new List<ICriterion>();
        IList<ICriterion> queryList1 = new List<ICriterion>();
        // 排序字段
        Order order;

        /// <summary>
        /// 计划类别 Id
        /// </summary>
        protected Int32 PID { get { int _pid = 0; int.TryParse(base.QueryString("pid"), out _pid); return _pid; } }
        /// <summary>
        /// 计划类别
        /// </summary>
        protected string JhType { get; set; }
        protected string Jlyear { get; set; }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            JhType = base.QueryString("jhtype");
            Jlyear = base.QueryString("jlyear");

            
            if (!IsPostBack)
            {
                
                //queryList1.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Plantable>(d => d.Jlyear), Jlyear));
                IQueryable<Plantable> list1 = ManagerFactory.PlantableManager.GetAll();
                
                DropDownList1.DataSource = list1;
                foreach (Plantable item in list1)
                {
                    if (DropDownList1.Items.IndexOf(new ListItem(item.Jlyear, item.Jlyear)) == -1)
                        DropDownList1.Items.Add(new ListItem(item.Jlyear, item.Jlyear));
                }


                DropDownList1.Items.Insert(0, new ListItem("全部", ""));


                IQueryable<DPlantable> list = ManagerFactory.DPlantableManager.GetAll();
                list = list.Where<DPlantable>(d => d.Jlyear == DateTime.Now.Year.ToString());
                JhTypeDropDownList.DataSource = list;
                foreach (DPlantable item in list)
                {
                    if (JhTypeDropDownList.Items.IndexOf(new ListItem(item.JhType, item.JhType)) == -1)
                        JhTypeDropDownList.Items.Add(new ListItem(item.JhType, item.JhType));

                }
                JhTypeDropDownList.Items.Insert(0, new ListItem("全部", ""));
              
                //JhTypeDropDownList.DataTextField = "JhType";
                //JhTypeDropDownList.DataTextField = "JhType";
                //JhTypeDropDownList.DataBind();
                
                
                BindGrid();
           }
        }
        #region 搜索

        protected void searchButton_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void QuerySelect()
        {
           
            // 传入条件
            // 添加自定义条件
            //if (PID>0)
            //    queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Plantable>(d => d.JhType), PID));
            if (JhTypeDropDownList.Items.FindByValue(JhType) != null)
            {
                JhTypeDropDownList.ClearSelection();
                JhTypeDropDownList.Items.FindByValue(JhType).Selected = true;
            }
            if (!string.IsNullOrEmpty(JhType))
                queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Plantable>(d => d.JhType), JhType));

            if (DropDownList1.Items.FindByValue(Jlyear) != null)
            {
                DropDownList1.ClearSelection();
                DropDownList1.Items.FindByValue(Jlyear).Selected = true;
            }
            if (!string.IsNullOrEmpty(Jlyear))
                queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Plantable>(d => d.Jlyear), Jlyear));

            if (!string.IsNullOrEmpty(txt_Jh_name.Text))
                queryList.Add(Restrictions.Like(PropertyNameHelper.Resolve<Plantable>(d => d.Jhname), txt_Jh_name.Text.Trim(), MatchMode.Anywhere));
            if (Request.QueryString["jlyear"] != null)
            {
                queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Plantable>(d => d.Jlyear),Request.QueryString["jlyear"].ToString()));
            }

        }

        #endregion

        private void BindGrid()
        {
           
            this.QuerySelect();
            AspNetPager1.RecordCount = ManagerFactory.PlantableManager.GetCount(queryList);
            IList<Plantable> list = ManagerFactory.PlantableManager.GetPageList(AspNetPager1, queryList, order);
            GridView1.DataSource = list;
            GridView1.DataBind();
        }
   


        #region gridview绑定及命令事件

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            object itemId = e.CommandArgument;
            if (e.CommandName == "DeleteIt")
            {
                #region 约束检查
                bool IsExistFK = false;
                //创建查询器
                Plantable model = ManagerFactory.PlantableManager.Get(int.Parse(itemId.ToString()));
                if (model == null)
                {
                    ExecJsAjax("alert('记录不存在！');", Page);
                    return;
                }
                else
                {
                    IList<ICriterion> delqueryList = new List<ICriterion>();
                    delqueryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Itemtable>(d => d.Jhname), model.Jhname));
                    int count = ManagerFactory.ItemtableManager.GetCount(delqueryList);
                    IsExistFK = count > 0;
                }
                #endregion
                if (!IsExistFK)
                {
                    ManagerFactory.PlantableManager.Delete(model);
                    BindGrid();
                }
                else
                {
                    ExecJsAjax("alert('该记录已经被使用,不能删除！');", Page);
                }
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //string cssName;
                //if (e.Row.RowState == DataControlRowState.Alternate) cssName = "grid_alt_item";
                //else cssName = "grid_item";
                //e.Row.Attributes.Add("onmouseout", "this.className='" + cssName + "'");
                //e.Row.Attributes.Add("onmouseover", "this.className='" + "gridSelectedItem" + "'");
            }
        }

        #endregion

        #region 分页

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        #endregion

        #region 排序

        public string SortD
        {
            get
            {
                if (ViewState["SortD"] == null)
                {
                    switch (GridView1.SortDirection)
                    {
                        case SortDirection.Ascending:
                            ViewState["SortD"] = "ASC";
                            break;
                        case SortDirection.Descending:
                            ViewState["SortD"] = "DESC";
                            break;
                    }
                }
                return Convert.ToString(ViewState["SortD"]);
            }
            set
            {
                ViewState["SortD"] = value;
            }
        }
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            SortD = (SortD.ToLower() == "asc" ? "DESC" : "ASC");
            order = new Order(e.SortExpression,SortD.ToLower()=="ASC".ToLower());
        }
        protected void GridView1_Sorted(object sender, EventArgs e)
        {
            BindGrid();
        }
       
        #endregion

        protected void JhTypeDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("List.aspx?JhType="+JhTypeDropDownList.SelectedValue);
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("List.aspx?Jlyear="+DropDownList1.SelectedValue);
            
        }
    }
}
